/*
 *  Copyright (C) 2005-2007 Renier Roth
 *  www.renier.de
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.renier.jkeepass.dialog;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import de.renier.jkeepass.Messages;
import de.renier.jkeepass.util.LocalProperties;
import de.renier.jkeepass.util.Utils;


/**
 * PreferencesDialog
 *
 * @author <a href="mailto:software@renier.de">Renier Roth</a>
 */
public class PreferencesDialog extends JDialog {

  private static final long serialVersionUID = 1L;
  public static final int RESULT_CANCEL = 0;
  public static final int RESULT_OK = 1;
  private JPanel jContentPane = null;
  private JPanel jPanel = null;
  private JPanel jPanel1 = null;
  private JButton saveButton = null;
  private JButton cancelButton = null;
  private JLabel jLabel = null;
  private JCheckBox quickOpenCheckBox = null;
  private JLabel jLabel1 = null;
  private JComboBox layoutComboBox = null;
  private JLabel jLabel2 = null;
  private JCheckBox decorateWindowCheckBox = null;
  private JLabel jLabel3 = null;
  private JComboBox languageComboBox = null;

  /**
   * @param owner
   */
  public PreferencesDialog(Frame owner) {
    super(owner);
    initialize();
  }

  /**
   * This method initializes this
   * 
   * @return void
   */
  private void initialize() {
    this.setSize(391, 190);
    this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    this.setModal(true);
    this.setTitle(Messages.getString("PreferencesDialog.0")); //$NON-NLS-1$
    this.setContentPane(getJContentPane());
    this.setLocationRelativeTo(this.getOwner());
  }

  /**
   * This method initializes jContentPane
   * 
   * @return javax.swing.JPanel
   */
  private JPanel getJContentPane() {
    if (jContentPane == null) {
      jContentPane = new JPanel();
      jContentPane.setLayout(new BorderLayout());
      jContentPane.add(getJPanel(), BorderLayout.SOUTH);
      jContentPane.add(getJPanel1(), BorderLayout.CENTER);
    }
    return jContentPane;
  }

  /**
   * This method initializes jPanel	
   * 	
   * @return javax.swing.JPanel	
   */
  private JPanel getJPanel() {
    if (jPanel == null) {
      jPanel = new JPanel();
      jPanel.setLayout(new FlowLayout());
      jPanel.add(getSaveButton(), null);
      jPanel.add(getCancelButton(), null);
    }
    return jPanel;
  }

  /**
   * This method initializes jPanel1	
   * 	
   * @return javax.swing.JPanel	
   */
  private JPanel getJPanel1() {
    if (jPanel1 == null) {
      GridBagConstraints gridBagConstraints22 = new GridBagConstraints();
      gridBagConstraints22.fill = GridBagConstraints.HORIZONTAL;
      gridBagConstraints22.gridy = 3;
      gridBagConstraints22.weightx = 1.0;
      gridBagConstraints22.insets = new Insets(0, 0, 0, 5);
      gridBagConstraints22.gridx = 1;
      GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
      gridBagConstraints13.gridx = 0;
      gridBagConstraints13.anchor = GridBagConstraints.WEST;
      gridBagConstraints13.gridy = 3;
      gridBagConstraints13.insets = new Insets(0, 5, 0, 5);
      jLabel3 = new JLabel();
      jLabel3.setText(Messages.getString("PreferencesDialog.7"));
      GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
      gridBagConstraints21.gridx = 1;
      gridBagConstraints21.anchor = GridBagConstraints.WEST;
      gridBagConstraints21.gridy = 2;
      GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
      gridBagConstraints12.gridx = 0;
      gridBagConstraints12.anchor = GridBagConstraints.WEST;
      gridBagConstraints12.insets = new Insets(0, 5, 0, 5);
      gridBagConstraints12.gridy = 2;
      jLabel2 = new JLabel();
      jLabel2.setText(Messages.getString("PreferencesDialog.1")); //$NON-NLS-1$
      GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
      gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
      gridBagConstraints2.gridy = 1;
      gridBagConstraints2.weightx = 1.0;
      gridBagConstraints2.insets = new Insets(0, 0, 0, 5);
      gridBagConstraints2.gridx = 1;
      GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
      gridBagConstraints11.gridx = 0;
      gridBagConstraints11.anchor = GridBagConstraints.WEST;
      gridBagConstraints11.insets = new Insets(0, 5, 0, 5);
      gridBagConstraints11.gridy = 1;
      jLabel1 = new JLabel();
      jLabel1.setText(Messages.getString("PreferencesDialog.2")); //$NON-NLS-1$
      GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
      gridBagConstraints1.gridx = 1;
      gridBagConstraints1.anchor = GridBagConstraints.WEST;
      gridBagConstraints1.gridy = 0;
      GridBagConstraints gridBagConstraints = new GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.anchor = GridBagConstraints.WEST;
      gridBagConstraints.insets = new Insets(0, 5, 0, 5);
      gridBagConstraints.gridy = 0;
      jLabel = new JLabel();
      jLabel.setText(Messages.getString("PreferencesDialog.3")); //$NON-NLS-1$
      jPanel1 = new JPanel();
      jPanel1.setLayout(new GridBagLayout());
      jPanel1.add(jLabel, gridBagConstraints);
      jPanel1.add(getQuickOpenCheckBox(), gridBagConstraints1);
      jPanel1.add(jLabel1, gridBagConstraints11);
      jPanel1.add(getLayoutComboBox(), gridBagConstraints2);
      jPanel1.add(jLabel2, gridBagConstraints12);
      jPanel1.add(getDecorateWindowCheckBox(), gridBagConstraints21);
      jPanel1.add(jLabel3, gridBagConstraints13);
      jPanel1.add(getLanguageComboBox(), gridBagConstraints22);
    }
    return jPanel1;
  }

  /**
   * This method initializes saveButton	
   * 	
   * @return javax.swing.JButton	
   */
  private JButton getSaveButton() {
    if (saveButton == null) {
      saveButton = new JButton();
      saveButton.setText(Messages.getString("PreferencesDialog.4")); //$NON-NLS-1$
      saveButton.setIcon(new ImageIcon(getClass().getResource("/org/javalobby/icons/20x20/Save.gif"))); //$NON-NLS-1$
      saveButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          // save preferences
          LocalProperties.getInstance().setProperty(LocalProperties.PROP_QUICKOPEN, String.valueOf(getQuickOpenCheckBox().isSelected()));
          LocalProperties.getInstance().setProperty(LocalProperties.PROP_SYSTEM_STYLE_DECORATE, String.valueOf(getDecorateWindowCheckBox().isSelected()));
          // language
          LocalProperties.getInstance().setProperty(LocalProperties.PROP_SYSTEM_LANGUAGE, String.valueOf(languageComboBox.getSelectedItem()));
          
          // set style if changed
          String selectedStyle = String.valueOf(getLayoutComboBox().getSelectedItem());
          if (!selectedStyle.equals(LocalProperties.getInstance().getProperty(LocalProperties.PROP_SYSTEM_STYLE))) {
            LocalProperties.getInstance().setProperty(LocalProperties.PROP_SYSTEM_STYLE, selectedStyle);
            // trigger system style change
            Utils.styleChange(selectedStyle);
          }          
          // write properties
          LocalProperties.getInstance().storeLocalProps();
          // dispose dialog
          dispose();
        }
      });
    }
    return saveButton;
  }

  /**
   * This method initializes cancelButton	
   * 	
   * @return javax.swing.JButton	
   */
  private JButton getCancelButton() {
    if (cancelButton == null) {
      cancelButton = new JButton();
      cancelButton.setText(Messages.getString("PreferencesDialog.6")); //$NON-NLS-1$
      cancelButton.setIcon(new ImageIcon(getClass().getResource("/org/javalobby/icons/20x20/Stop.gif"))); //$NON-NLS-1$
      cancelButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          dispose();
        }
      });
    }
    return cancelButton;
  }

  /**
   * This method initializes quickOpenCheckBox	
   * 	
   * @return javax.swing.JCheckBox	
   */
  private JCheckBox getQuickOpenCheckBox() {
    if (quickOpenCheckBox == null) {
      quickOpenCheckBox = new JCheckBox();
      // read from properties
      String quickOpen = LocalProperties.getInstance().getProperty(LocalProperties.PROP_QUICKOPEN);
      quickOpenCheckBox.setSelected(Boolean.valueOf(quickOpen).booleanValue());
    }
    return quickOpenCheckBox;
  }

  /**
   * This method initializes layoutComboBox	
   * 	
   * @return javax.swing.JComboBox	
   */
  private JComboBox getLayoutComboBox() {
    if (layoutComboBox == null) {
      layoutComboBox = new JComboBox();
      layoutComboBox.addItem("default"); //$NON-NLS-1$
      // read from resource
      try {
        InputStream input = PreferencesDialog.class.getResourceAsStream("/de/renier/jkeepass/resources/themepacks/themes.properties"); //$NON-NLS-1$
        if (input != null) {
          BufferedReader buffer = new BufferedReader(new InputStreamReader(input));
          String line;
          while ((line = buffer.readLine()) != null) {
            layoutComboBox.addItem(line);
          }
        }
      } catch (IOException e) {
        // nothing
      }
      String style = LocalProperties.getInstance().getProperty(LocalProperties.PROP_SYSTEM_STYLE);
      layoutComboBox.setSelectedItem(style);
      layoutComboBox.addItemListener(new java.awt.event.ItemListener() {
        public void itemStateChanged(java.awt.event.ItemEvent e) {
          String style = String.valueOf(getLayoutComboBox().getSelectedItem());
          Utils.styleChange(style);
          // update this dialog as well
          SwingUtilities.updateComponentTreeUI(PreferencesDialog.this);
        }
      });
    }
    return layoutComboBox;
  }

  /**
   * This method initializes decorateWindowCheckBox	
   * 	
   * @return javax.swing.JCheckBox	
   */
  private JCheckBox getDecorateWindowCheckBox() {
    if (decorateWindowCheckBox == null) {
      decorateWindowCheckBox = new JCheckBox();
      // read from properties
      String decorateWindow = LocalProperties.getInstance().getProperty(LocalProperties.PROP_SYSTEM_STYLE_DECORATE);
      decorateWindowCheckBox.setSelected(Boolean.valueOf(decorateWindow).booleanValue());
    }
    return decorateWindowCheckBox;
  }

  /**
   * This method initializes languageComboBox	
   * 	
   * @return javax.swing.JComboBox	
   */
  private JComboBox getLanguageComboBox() {
    if (languageComboBox == null) {
      languageComboBox = new JComboBox();
      languageComboBox.addItem("de");
      languageComboBox.addItem("en");
      
      // preset
      String language = LocalProperties.getInstance().getProperty(LocalProperties.PROP_SYSTEM_LANGUAGE);
      if (!Utils.isEmpty(language)) {
        languageComboBox.setSelectedItem(language);
      }
      
    }
    return languageComboBox;
  }

}  //  @jve:decl-index=0:visual-constraint="10,10"
